package home.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.layout.SingleLayoutHelper;
import com.bumptech.glide.Glide;

import net.ck.hzq.R;
import net.ck.hzq.TobuyApplication;
import net.ck.hzq.WebShareActivity;

import java.util.ArrayList;
import java.util.List;

import Utils.ScreenUtil;
import base.BasePagerAdapter;
import ui.activity.home.CreditCardsAct;
import ui.model.CreditCardsBean;
import util.ToastUtils;

/**
 * @author BF00233
 */

public class HotRecommendAdapter extends DelegateAdapter.Adapter<HotRecommendAdapter.BannerViewHolder> {

    private final Context mContext;

    private List<CreditCardsBean> mData;

    public HotRecommendAdapter(Context context) {
        mContext = context;
        mData = new ArrayList<>();
    }

    public void setUpBanner(List<CreditCardsBean> data) {
        mData.clear();
        if (data != null) {
            mData.addAll(data);
        }
        notifyDataSetChanged();
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        SingleLayoutHelper layoutHelper = new SingleLayoutHelper();
        layoutHelper.setMargin(0, ScreenUtil.dip2px(mContext, 5), 0, ScreenUtil.dip2px(mContext, 5));
        return layoutHelper;
    }

    @NonNull
    @Override
    public BannerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.item_home_hot_recommend, parent, false);
        return new BannerViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull final BannerViewHolder holder, int position) {
        holder.more.setOnClickListener(v -> {
            if (!TobuyApplication.isAuthed()) {
                ToastUtils.shortToast("请先进行实名认证");
                return;
            }
            Intent intent = new Intent(mContext, CreditCardsAct.class);
            mContext.startActivity(intent);
        });
        if (mData == null) return;
        BannerPagerAdapter adapter = new BannerPagerAdapter(mData.size());
        holder.viewPager.setAdapter(adapter);

    }

    @Override
    public int getItemCount() {
        return 1;
    }

    class BannerViewHolder extends RecyclerView.ViewHolder {
        private ViewPager viewPager;
        private TextView more;

        private BannerViewHolder(View itemView) {
            super(itemView);
            viewPager = itemView.findViewById(R.id.view_pager);
            more = itemView.findViewById(R.id.more_info);
            viewPager.setPageMargin(ScreenUtil.dip2px(itemView.getContext(), 10));
//            viewPager.setOffscreenPageLimit(3);
        }
    }

    private class BannerPagerAdapter extends BasePagerAdapter {

        BannerPagerAdapter(int count) {
            super(count);
        }

        @Override
        public float getPageWidth(int position) {
            return 0.98f;
        }

        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, final int index) {
            ImageView imageView = new ImageView(container.getContext());
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            Glide.with(container.getContext())
                    .load(mData.get(index).getImg())
                    .into(imageView);
            container.addView(imageView);
            imageView.setOnClickListener(v -> {
                if (!TobuyApplication.isAuthed()) {
                    ToastUtils.shortToast("请先进行实名认证");
                    return;
                }
                if (mData.get(index).getIsParent().equals("1")) {
                    Intent intent = new Intent(mContext, CreditCardsAct.class);
                    intent.putExtra("info", mData.get(index));
                    mContext.startActivity(intent);
                } else {
                    Intent intent = new Intent(mContext, WebShareActivity.class);
                    intent.putExtra("data", mData.get(index));
                    mContext.startActivity(intent);
                }

            });
            return imageView;
        }
    }

}
